package jsyf.oa.service.wechat.service_account;

import com.alibaba.fastjson.JSONObject;
import jsyf.oa.facade.wechat.config.WeChatConst;
import jsyf.oa.facade.wechat.service_account.AccountMenuFacade;
import jsyf.oa.facade.wechat.service_company.TokenFacade;
import jsyf.oa.service.wechat.service_company.HttpUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
@Transactional(rollbackFor = Exception.class)
@com.alibaba.dubbo.config.annotation.Service(protocol = {"dubbo"})
public class AccountMenuService implements AccountMenuFacade {

    @Autowired
    private TokenFacade tokenFacade;

    @Override
    public JSONObject createMenu(String jsonData) {

        String accessToken = tokenFacade.getServiceAccessToken(WeChatConst.weChat_account_type);
        // 拼装接口url
        String url = WeChatConst.menu_create_url.replace("ACCESS_TOKEN", accessToken);
        // 调用接口
        JSONObject jsonObject = HttpUtil.request(url, "POST", jsonData);

        return jsonObject;
    }

    @Override
    public JSONObject getMenu() {

        String accessToken = tokenFacade.getServiceAccessToken(WeChatConst.weChat_account_type);
        // 拼装接口url
        String url = WeChatConst.menu_get_url.replace("ACCESS_TOKEN", accessToken);
        // 调用接口
        JSONObject jsonObject = HttpUtil.request(url, "GET", null);

        return jsonObject;
    }

    @Override
    public JSONObject deleteMenu() {

        String accessToken = tokenFacade.getServiceAccessToken(WeChatConst.weChat_account_type);
        // 拼装接口url
        String url = WeChatConst.menu_delete_url.replace("ACCESS_TOKEN", accessToken);
        // 调用接口
        JSONObject jsonObject = HttpUtil.request(url, "GET", null);

        return jsonObject;
    }
}
